package com.example.order.activity;

import android.annotation.SuppressLint;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.order.R;
import com.example.order.pojo.Customer;
import com.example.order.utils.Utils;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Main6Activity extends AppCompatActivity implements View.OnClickListener {
    EditText edit1;
    EditText edit2;
    Customer customer;
    TextView text1;
    TextView text2;
    TextView text3;
    TextView text4;
    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main6);
        text1=findViewById(R.id.my_text1);//编号、姓名
        text2=findViewById(R.id.my_text2);//手机号码 账号余额
        text3=findViewById(R.id.my_text3);//口味偏好
        text4=findViewById(R.id.my_text4);//忌口信息
        edit1=findViewById(R.id.my_edit1);
        edit2=findViewById(R.id.my_edit2);
        Button btn1=findViewById(R.id.my_btn1);//修改口味
        Button btn2=findViewById(R.id.my_btn2);//修改偏好
        Button btn3=findViewById(R.id.my_btn3);//修改密码
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        //查看个人信息
        customer = (Customer)getIntent().getSerializableExtra("customer");
        assert customer != null;
        Log.i("Main6Activity",customer.toString());
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

            }
        });
       String str1="编号:"+customer.getId().toString()+
                " 姓名:"+customer.getName()+
                " 余额:"+customer.getBalance().toString();
        text1.setText(str1);
        text2.setText("手机号码:"+customer.getPhone());
        text3.setText("口味偏好:"+customer.getFlavor());
        text4.setText("忌口信息:"+customer.getTaboo());


    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.my_btn1:
                //修改口味
                final String str1=edit1.getText().toString();
                if(Utils.isNotNullString(str1))
                {
                    //发送给服务器
                    HashMap<String,String> map1=new HashMap<>();
                    map1.put("id",customer.getId().toString());
                    map1.put("flavor",str1);
                    Utils.sendPostHttpRequest("/customer/set",
                            map1,
                            new Callback() {
                                @Override
                                public void onFailure(Call call, IOException e) {

                                }

                                @Override
                                public void onResponse(Call call, Response response) throws IOException {
                                    final String string = response.body().string();
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Utils.showToast(Main6Activity.this,
                                                    "修改成功,服务器返回的消息:"+string,
                                                    false);
                                            text3.setText("口味偏好:"+str1);
                                        }
                                    });


                                }
                            });

                }
                else   Utils.showToast(Main6Activity.this,"不能为空",true);
                break;
            case R.id.my_btn2:
                //修改忌口
                final String str2=edit1.getText().toString();
                if(Utils.isNotNullString(str2))
                {
                    HashMap<String,String> map2=new HashMap<>();
                    map2.put("id",customer.getId().toString());
                    map2.put("taboo",str2);
                    Utils.sendPostHttpRequest("/customer/set",
                            map2,
                            new Callback() {
                                @Override
                                public void onFailure(Call call, IOException e) {

                                }

                                @Override
                                public void onResponse(Call call, Response response) throws IOException {
                                    final String string2 = response.body().string();
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Utils.showToast(Main6Activity.this,
                                                    "修改成功,服务器返回的消息:"+string2,
                                                    false);
                                            text4.setText("口味偏好:"+str2);

                                        }
                                    });
                                }
                            });
                }
                else   Utils.showToast(Main6Activity.this,"不能为空",true);
                break;
            case R.id.my_btn3:
                //修改密码
                String pass=edit2.getText().toString();
                if(Utils.isNotNullString(pass))
                {
                    HashMap<String,String> map=new HashMap<>();
                    map.put("phone",customer.getPhone());
                    map.put("oldPass",customer.getPassword());
                    map.put("newPass",pass);
                    Utils.sendPostHttpRequest("/customer/setPass",
                            map,
                            new Callback() {
                                @Override
                                public void onFailure(Call call, IOException e) {

                                }

                                @Override
                                public void onResponse(Call call, Response response) throws IOException {
                                    final String string = response.body().string();
                                    if (string.equals("修改成功")) {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Utils.showToast(Main6Activity.this,
                                                        "密码修改成功", false);
                                            }
                                        });
                                    }
                                    else
                                    {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Utils.showToast(Main6Activity.this,
                                                        string, false);
                                            }});
                                    }

                                }
                            });

                }
                else
                Utils.showToast(Main6Activity.this,"密码不能为空",true);
                break;
            default:
                break;
        }

    }
}
